package Tools;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseConn {
    Statement st;
    private static Connection conn = null;
     public static Connection getCon() {
         try {
             Class.forName("com.mysql.jdbc.Driver"); //加载数据库连接驱动
             String user = "root";
             String pwd = "123456";  //XXX为自己的数据库的密码
             String url = "jdbc:mysql://192.168.100.240:3306/interfacetest?useUnicode=true&characterEncoding=UTF-8";                   //ZZZ为连接的名字
             conn = DriverManager.getConnection(url, user, pwd);  //获取连接
             //System.out.println("数据库连接成功");
         } catch (Exception e) {
             System.out.println("连接数据库失败");
             e.printStackTrace();
         }
         return conn;
     }
      /*public static void main(String[] args){
         Connection a ;
         a = DatabaseConn.getCon();
         System.out.println(a);
      }*/

    public  void add(String interfaceName,String path, String Protocol,String request,String ExceptResult,String actualResult,String responseTime,String result) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
        // 定义sql语句
        //String sql1="insert into Table2(id,name,grade) values('20121114','大学英语',3)";
        //String sql2="insert into test2 (id,name,grade) values('20121115','tiyu',2)";
        String sql3="insert into response (interface_name,Path,Protocol,Request,ExceptResult,ActualResult,Result,ResponseTime) values("+"\""+interfaceName+"\","+"\""+path+"\","+"\""+Protocol+"\","+"\""+request+"\","+"\""+ExceptResult+"\","+"\""+actualResult+"\","+"\""+result+"\","+"\""+responseTime+"\")";
        // 3、创建语句对象
        st =getCon().createStatement();
        // st.executeUpdate(sql1);
        //st.executeUpdate(sql2);
        st.executeUpdate(sql3);
        // 4、遍历结果集：此处插入记录不需要
        // 5、关闭资源对象
        st.close();
        getCon().close();
    }
    public  void update(String database,String name, String value) throws ClassNotFoundException, SQLException {
        // 定义sql语句
        String sql1="update "+database+" set "+name+"="+value;
        // 3、创建语句对象
        st =getCon().createStatement();
        st.executeUpdate(sql1);

        // 4、遍历结果集：此处插入记录不需要
        // 5、关闭资源对象
        st.close();
        getCon().close();
    }
    public  void delete() throws ClassNotFoundException, SQLException {
        // 定义sql语句
        String sql1="delete Table2 where id='20121115'";
        String sql2="delete Table2 where id='20121116'";
        // 3、创建语句对象
        st =getCon().createStatement();
        st.executeUpdate(sql1);
        st.executeUpdate(sql2);
        // 4、遍历结果集：此处插入记录不需要
        // 5、关闭资源对象
        st.close();
        getCon().close();
    }

    public  void select() throws ClassNotFoundException, SQLException {
        // 定义sql语句
        String sql1="select * from response where ";
        // 3、创建语句对象
        st =getCon().createStatement();
        st.executeUpdate(sql1);

        // 4、遍历结果集：此处插入记录不需要
        // 5、关闭资源对象
        st.close();
        getCon().close();
    }

}
